import Router from '@koa/router';
import response from '../response.js';

import device from '../mqtt.js'




const router = new Router;

router.post('/api/device/openFan', async (ctx) => {
    let body = ctx.request.body;
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    let res = await device.openFan(body.sn);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }
});

router.post('/api/device/closeFan', async (ctx) => {
    let body = ctx.request.body;
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    let res = await device.closeFan(body.sn);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }
});
router.post('/api/device/restart', async (ctx) => {
    let body = ctx.request.body;
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    let res = await device.restart(body.sn);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }
});

router.post('/api/device/startpurification', async (ctx) => {
    let body = ctx.request.body;
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    let res = await device.startpurification(body.sn);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }
});

router.post('/api/device/stoppurification', async (ctx) => {
    let body = ctx.request.body;
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    let res = await device.stoppurification(body.sn);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }
});

router.post('/api/device/setinterval', async (ctx) => {
    let body = ctx.request.body;
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    if(body.interval == null || body.interval == undefined){
        ctx.body = response.fail("interval不允许为空");
        return;
    }
    let res = await device.setinterval(body.sn,body.interval);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }
});
router.post('/api/device/queryStatus', async (ctx) => {
    let body = ctx.request.body;
    console.log('/api/device/queryStatus',body)
    if(body == null){
        ctx.body = response.fail("参数不允许为空");
        return;
    }
    if(body.sn == null || body.sn == undefined){
        ctx.body = response.fail("sn不允许为空");
        return;
    }
    let res = await device.queryStatus(body.sn);
    if(res.code==1){
        ctx.body = response.success();
    }
    else{
        ctx.body = response.fail(res.message);
    }

});


export { router };